<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\meter;

class meterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(){
        return view('meter');
    }
    public function sign(Request $request){
        $u_id = $request->input('u_id');
        $meterInfo = meter::getSignMeter($u_id);
        if(empty($meterInfo)){
            $meter = 10;
        }else{
            $time = $meterInfo->time;
            if($time == date('Y-m-d',time())){
                return response()->json([
                    'code'=>'201',
                    'msg' => '您已经签到过了'
                ]);
            }
            $now = date('Y-m-d',strtotime('-1 day'));
            if($now == $time){
                $meter = $meterInfo->meters + 10;
            }else{
                $meter = 10;
            }
        }
        $res = meter::meterSignInsert($u_id,$meter);
        if($res){
            return response()->json([
                'code'=>'200',
                'msg' => '签到成功'
            ]);
        }else{
            return response()->json([
                'code'=>'201',
                'msg' => '签到失败'
            ]);
        }
    }
}
